package com.tw.wechart2.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.tw.wechart2.R;
import com.tw.wechart2.constant.Constant;
import com.tw.wechart2.util.GlideUtil;
import com.tw.wechart2.util.Util;

import java.util.List;

/**
 * @Description: tweet图片展示adapter
 * @Author: lanteng 2020/9/16
 * @Version: 1.0
 */
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {

    private Context context;
    // 图片列表
    private List<String> listImage;

    public ImageAdapter(Context context, List<String> listImage) {
        this.context = context;
        this.listImage = listImage;
    }

    @NonNull
    @Override
    public ImageAdapter.ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_image, parent, false);
        return new ImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageAdapter.ImageViewHolder holder, int position) {
        String url = listImage.get(position);
        ViewGroup.LayoutParams params = holder.iv_image.getLayoutParams();
        int width = Util.instance().getScreenWidth(context) * 3/5;
        params.width = (width-12)/3;
        params.height = (width-12)/3;
        holder.iv_image.setLayoutParams(params);
        GlideUtil.instance().displayImgage(context, url, holder.iv_image);
    }

    @Override
    public int getItemCount() {
        return (listImage == null ? 0 : listImage.size());
    }

    class ImageViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_image;

        public ImageViewHolder(View itemView) {
            super(itemView);
            iv_image = itemView.findViewById(R.id.iv_image);
        }
    }

}
